<template>
	<view class="home">
		<view class="scrollNav">
			<scroll-view scroll-x class="navscroll">
				<view class="item" :class="{'active':navIndex===index}" v-for="(item,index) in navList" :key="item.id"
					@click="clicknav(index,item.id)">{{item.classname}}</view>
			</scroll-view>
		</view>
		<view class="content">
			<view class="row" v-for="item in NewsList">
				<newsbox @click.native="goDetail(item)" :item=item></newsbox>
			</view>
		</view>
		<view class="nodata" v-if="!NewsList.length">
			<image src="../../static/images/nodata.png" mode="widthFix"></image>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navIndex: 0,
				navList: [],
				NewsList: [],
				currentPage: 1,
				loadding: 0
			}
		},
		onLoad() {
			this.getNavData()
			this.getNewsData()
		},
		onReachBottom() {
			if (this.loadding === 0) {
				uni.showLoading({
					title: '加载中',
					mask: true

				})
				setTimeout(function() {
					uni.hideLoading();
				}, 500);
				this.currentPage++
				this.getNewsData()
			}
		},
		methods: {
			clicknav(index, id) {
				this.currentPage = 1
				this.NewsList = []
				this.navIndex = index
				this.getNewsData(id)
			},
			goDetail(item) {
				
				uni.navigateTo({
					url: `/pages/detail/detail?cid=${item.classid}&id=${item.id}`
				})
			},
			getNavData() {
				uni.request({
					url: "https://ku.qingnian8.com/dataApi/news/navlist.php",
					success: res => {
						this.navList = res.data
					}
				})
				
			},
			getNewsData(id) {
				uni.request({
					url: "https://ku.qingnian8.com/dataApi/news/newslist.php",
					data: {
						cid: id || 50,
						page: this.currentPage
					},
					success: res => {
						if (res.data.length === 0) {
							uni.showToast({
									icon: 'error',
									title: '没有更多啦~',
									mask: true,
									duration: 500
								}),
								this.loadding = 1
						}
						this.loadding = 0
						this.NewsList = [...this.NewsList, ...res.data]
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.active {
		color: #31c27c,
	}

	.home {
		.navscroll {
			height: 100rpx;
			background-color: #f7f8fa;
			white-space: nowrap;
			position: fixed;
			top: var(--window-top);
			z-index: 1;

			// left:0;
			/deep/ ::-webkit-scrollbar {
				width: 4px !important;
				height: 1px !important;
				overflow: auto !important;
				background: transparent !important;
				-webkit-appearance: auto !important;
				display: block;
			}

			.item {
				font-size: 40rpx;
				display: inline-block;
				line-height: 100rpx;
				padding: 0 30rpx;
				color: #333;
			}

			.active {
				color: #31c27c;
			}
		}

		.content {
			padding: 30rpx;
			padding-top: 130rpx;

			.row {
				border-bottom: 1px dashed #efefef;
				padding: 15rpx 0;
			}
		}

		.nodata {
			display: flex;
			justify-content: center;

			image {
				width: 360rpx;
			}
		}
	}
</style>
